﻿using log4net;
using Mate.Caching;
using Mate.Core.Base;
using Mate.Core.Configuration;
using Mate.Db.Model.Info;
using Mate.Framework.BaseController.Api;
using Mate.Merchant.Service;
using Mate.Other.Api.Controller.Models.Banner;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;

namespace Mate.Other.Api.Controller.Controller
{
    public class BannerController: OtherBaseController
    {
        private readonly ILog log;
        private readonly ICacheManager cache;
        private readonly MateConfig mateConfig;
        private readonly IBannerService bannerService;

        public BannerController(ILog log, ICacheManager cache, MateConfig mateConfig, IBannerService bannerService)
        {
            this.log = log;
            this.cache = cache;
            this.mateConfig = mateConfig;
            this.bannerService = bannerService;
        }

        #region 根据分类获取Banner
        /// <summary>
        /// 根据分类获取Banner
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResult<IList<mp_banner>> List(BannerListRequest req)
        {
            BaseResult<IList<mp_banner>> result = new BaseResult<IList<mp_banner>>();
            if (req.category < 1)
            {
                result.ResultCode = ResultCode.ParamError;
                result.Message = "参数错误";
                return result;
            }
            string key = string.Format("mate.banner.list-category{0}",req.category);
            var list = cache.Get<IList<mp_banner>>(key);
            if (list != null)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "OK";
                result.Data = list;
                return result;
            }

            var bannerList = bannerService.Queryable
                .Where(x => x.is_delete == 1 && x.is_open == 1&&x.category==req.category)
                .ToList();
            if (bannerList == null)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "暂无相关数据";
                return result;
            }

            result.ResultCode = ResultCode.Success;
            result.Message = "OK";
            result.Data = bannerList;
            cache.Set(key, result.Data, mateConfig.DefaultCacheTime);

            return result;

        }
        #endregion

    }
}
